const sequelize = require('../db')
const { DataTypes } = require('sequelize')
//创建一个模型对象
const Article = sequelize.define(
  'Article',
  {
    title: {
      type: DataTypes.STRING,
      allowNull: false
    },
    description: {
      type: DataTypes.STRING,
      allowNull: false
    },
    contentEditor: {
      type: DataTypes.TEXT,
      allowNull: false
    },
    img: {
      type: DataTypes.STRING
    },
    views: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },
    type: {
      type: DataTypes.STRING,
      allowNull: false
    },
    linkInput: {
      type: DataTypes.STRING,
      allowNull: true
    },
    openLevel: {
      type: DataTypes.STRING,
      allowNull: false
    },
    coverOrDescription: {
      type: DataTypes.STRING,
      allowNull: false
    },
    tabList: {
      type: DataTypes.STRING,
      get() {
        if (!this.getDataValue('tabList')) return
        return this.getDataValue('tabList').split(',')
      },
      set(value) {
        return this.setDataValue('tabList', value.join(','))
      }
    },
    comments: {
      type: DataTypes.STRING,
      get() {
        if (!this.getDataValue('comments')) return
        return this.getDataValue('comments').split(',')
      },
      set(value) {
        return this.setDataValue('comments', value.join(','))
      }
    }
  },
  {
    //   其他属性
    freezeTableName: true, // 不会在数据库表明加s
    paranoid: true //从此以后，该表的数据不会真正的删除，而是增加一列deletedAt，记录删除的时间
  }
)

module.exports = Article
